home *** CD-ROM | disk | FTP | other *** search
- From: Marco_Dalla-Gasperina@vcd.hp.com (Marco Dalla Gasperina)
- Message-ID: <4h5502$6gs@news.vcd.hp.com>
- X-Original-Date: Thu, 29 Feb 96 21:18:25 GMT
- Path: in2.uu.net!bounce-back
- Date: 01 Mar 96 14:06:12 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: Generic Object Callbacks
- Organization: HP
- References: <pgpmoose.199602221531.14635@isolde.mti.sgi.com> <4gsi2p$905@bcarh8ab.bnr.ca> <4guh9e$jt6@sdaw04.seinf.abb.se> <4h1pb6$n19@news2.cais.com> <3135C73D.5570@acf4.nyu.edu>
- X-Newsreader: News Xpress Version 1.0 Beta #4
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMTcEkOEDnX0m9pzZAQGUjwF/d7vYT3hypL1RfNBmnAyE9ERQeYS5BjKX
- s8DBP0+FxwYejp6T4BCXCYw+0aLLHCNi
- =91kC
-
- In article <3135C73D.5570@acf4.nyu.edu>, Shalom Reich <sqr1874@acf4.nyu.edu>
- wrote:
- >Ben Jones wrote:
- >>
- >> Anders Lindback (alindbac@sw.seisy.abb.se) wrote:
- >> : In article <4gsi2p$905@bcarh8ab.bnr.ca>,
- >> : brian (b.c.) white <bcwhite@bnr.ca> wrote:
- >> : >
- >> : >>What is needed is the ability to use member functions as callbacks
- >> : >>without any constraint on the type of the object they are invoked on.
- >> : >>This is possible in C++ only by using various implementation-dependent
- >> : >>hacks to escape the type system, because the language does not provide
- >> : >>any way to express such a construct.
- >> : >
-
- You really don't need hacks at all. It is completely possible to do it
- with the way the language is defined. See below.
-
- >I don't seem to understand the discussion in this thread. Yet it seems
- >to generate some strong opinions. So, . . .
- >
- >The routine that will issue the callback (let us call it the driver)
- >needs to use a stored pointer. The argument is that the stored pointer
- >should be to a member function.
- >Why isn't it good enough for the stored pointer to be a pointer to the
- >object and then the driver will only need to say "object->callback();" in
- >order to call the proper routine.
-
- The problem here is probably that you are constraining all "callbackable"
- objects to be derived from a common base class which may not be a natural
- attribute of the system.
-
- >The usual virtual function semantics would take care of any inheritance
- >hierarchy.
-
- Here's one way to do it (excuse any syntax errors as I haven't compiled
- this) ...
-
- class CallbackBase {
- public:
- virtual doit();
- };
-
- template<class T> class Callback : public CallbackBase {
- private:
- // the callback object saves a pointer to a member function
- // and a pointer to the target object.
- void (T::*callbackFunc)();
- T* target;
-
- public:
- // save the pointer-to-member and target object
- Callback( T* obj, void (T::*cbf)() ) : target(obj), callbackFunc(cbf) {}
- // doit issues the callback
- void doit() { target->*callbackFunc)(); }
- };
-
- Heres the driver code...
- DriverFunc( CallbackBase* cbb )
- {
- ...
- cbb->doit();
- ...
- delete cbb;
- }
-
- Heres the client code:
-
- class MyCleck {
- void Cleck() { ... }
- };
-
- class MyBlurp {
- void Blurp() { ... }
- };
-
- main()
- {
- ...
- MyCleck mc;
- MyBlurp mb;
-
- DriverFunc( new Callback<MyCleck>(&mc,&MyCleck::Cleck) );
- DriverFunc( new Callback<MyBlurp>(&mb,&MyBlurp::Blurp) );
- ...
- }
-
- I think this should work, as I have implemented the publisher/
- subscriber (observer/observed) pattern in an almost identical
- way.
-
- The problem is a little trickier trying to generalize any
- parameters that the callback methods require and still keep
- them typesafe.
-
- >Shalom Reich
-
- marco
- ---
- [ To submit articles: try just posting with your news-reader.
- If that fails, use mailto:std-c++@ncar.ucar.edu
- FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
- Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu.
- ]
-